package requests

import (
	"WeChatMGRA/modul"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func SendMsg(wId,wcid,content string,lenth int)error{
	msg:= new(modul.SendVoiceMsg)
	msg.WID = wId
	msg.WCID = wcid
	msg.Content = content
	msg.Length = lenth
	senddata,_:= json.Marshal(msg)
	req, err := http.NewRequest("POST", "http://134.175.73.113:8080/sendVoice", strings.NewReader(string(senddata)))
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlRWJLODcxRFZDTGhJbCh6TyIsImlzcyI6InhpbmdzaGVuZyIsImFjY291bnQiOiIxODcyMjYyNjExMCJ9._GDQ5kANgtC8Zon0_3BAp-Jz0k21H58Jq8e_CU4fjjc")
	if err != nil {
		fmt.Println("发送数据错误")
		return err
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println("发送请求错误")
	}
	data,err:=ioutil.ReadAll(res.Body)
	if err!=nil{
		fmt.Println("发送错误")
		return err
	}
	basemsg := new(modul.BaseMsg)
	err =json.Unmarshal(data,basemsg)
	if err!=nil{
		fmt.Println("获取数据解析错误")
		return err
	}
	if basemsg.Code == "1000"{
		return nil
	}
	return errors.New("发送失败")
}